package com.tebiecloud.order.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.common.utils.JsonUtils;
import com.tebiecloud.common.utils.RedisUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.order.client.constatns.OrderConstants;
import com.tebiecloud.order.client.model.entity.OrderStats2;
import com.tebiecloud.order.client.model.entity.Orderr;
import com.tebiecloud.order.server.mapper.OrderStats2Mapper;
import com.tebiecloud.order.server.service.OrderStats2Service;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.tebiecloud.store.client.constatns.StoreConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 门店营销额统计 服务实现类
 * @author tebie
 * @date 2019-09-23
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderStats2ServiceImpl extends BaseServiceImpl<OrderStats2Mapper, OrderStats2> implements OrderStats2Service {

    @Autowired
    OrderStats2Mapper orderStats2Mapper;

    @Autowired
    RedisUtils redisUtils;

    @Override
    public OrderStats2 createOrUpdate(Orderr order,int type,int online) {

        //获取今天的日期
        String today = DateUtils.formatDate(new Date(),"yyyy-MM-dd");
        String year = today.substring(0,4);
        String month = today.substring(0,7);

        int time = 10;
        String key = OrderConstants.REDIS_ORDERSTATS2_+order.getStoreId()+today;
        boolean isLock = redisUtils.lock(key, time);
        if(!isLock){
            throw new RuntimeException("收益记录被占用，请稍等");
        }
        //查询是否有今天的统计
        QueryWrapper<OrderStats2> orderStats2QueryWrapper = new QueryWrapper<>();
        orderStats2QueryWrapper.eq("day",today);
        orderStats2QueryWrapper.eq("store_id",order.getStoreId());
        OrderStats2 orderStats2 = orderStats2Mapper.selectOne(orderStats2QueryWrapper);
        log.info(">>minjie 获取当天orderStats2:"+orderStats2);
        int amount = 0;//营业总额，单位分
        int count = 0;//订单笔数
        int writeoffAmount = 0;//核销总金额，单位分
        int writeoffCount = 0;//核销笔数
        int cashAmount = 0;//现金营业额，单位分
        int cashCount = 0;//现金笔数
        int swipeAmount = 0;//刷卡营业额，单位分
        int swipeCount = 0;//刷卡笔数
        int onlineAmount = 0;//线上营业额，单位分
        int onlineCount = 0;//线上笔数
        int alipayAmount = 0;//线上营业额，单位分
        int alipayCount = 0;//线上笔数
        if(orderStats2!=null){
            OrderStats2 newOrderStats2 = new OrderStats2();
            amount = orderStats2.getAmount() + order.getPrice();
            count = orderStats2.getCount() + 1;
            if(type==1){
                writeoffAmount = orderStats2.getWriteoffAmount() + order.getPrice();
                writeoffCount = orderStats2.getWriteoffCount() + 1;
            }else{
                writeoffAmount = orderStats2.getWriteoffAmount();
                writeoffCount = orderStats2.getWriteoffCount();
            }
            if(online == 1){//线上收银
                onlineAmount = orderStats2.getOnlineAmount() + order.getPrice();
                onlineCount = orderStats2.getOnlineCount() + 1;
            }else{
                onlineAmount = orderStats2.getOnlineAmount();
                onlineCount = orderStats2.getOnlineCount();
            }
            if(order.getPayType() == 2){//现金支付
                cashAmount = orderStats2.getCashAmount() + order.getPrice();
                cashCount =  orderStats2.getCashCount() + 1;
            }else {
                cashAmount = orderStats2.getCashAmount();
                cashCount =  orderStats2.getCashCount();
            }
            if(order.getPayType() == 3){//刷卡支付
                swipeAmount = orderStats2.getSwipeAmount() + order.getPrice();
                swipeCount = orderStats2.getSwipeCount() + 1;
            }else{
                swipeAmount = orderStats2.getSwipeAmount();
                swipeCount = orderStats2.getSwipeCount();
            }
            if(order.getPayType() == 4){//支付宝支付
                alipayAmount = orderStats2.getAlipayAmount() + order.getPrice();
                alipayCount = orderStats2.getAlipayCount() + 1;
            }else{
                alipayAmount = orderStats2.getAlipayAmount();
                alipayCount = orderStats2.getAlipayCount();
            }
            newOrderStats2.setYear(year);
            newOrderStats2.setMonth(month);
            newOrderStats2.setDay(today);
            newOrderStats2.setAmount(amount);
            newOrderStats2.setCount(count);
            newOrderStats2.setWriteoffAmount(writeoffAmount);
            newOrderStats2.setWriteoffCount(writeoffCount);
            newOrderStats2.setCashAmount(cashAmount);
            newOrderStats2.setCashCount(cashCount);
            newOrderStats2.setSwipeAmount(swipeAmount);
            newOrderStats2.setSwipeCount(swipeCount);
            newOrderStats2.setOnlineAmount(onlineAmount);
            newOrderStats2.setOnlineCount(onlineCount);
            newOrderStats2.setAlipayAmount(alipayAmount);
            newOrderStats2.setAlipayCount(alipayCount);
            UpdateWrapper<OrderStats2> orderStats2UpdateWrapper = new UpdateWrapper<>();
            orderStats2UpdateWrapper.eq("store_id",order.getStoreId());
            orderStats2UpdateWrapper.eq("day",today);
            orderStats2Mapper.update(newOrderStats2,orderStats2UpdateWrapper);
        }else{
            orderStats2 = new OrderStats2();
            amount = order.getPrice();
            count = 1;
            if(type==1){
                writeoffAmount = order.getPrice();
                writeoffCount = 1;
            }
            if(online == 1){//线上收银
                onlineAmount = order.getPrice();
                onlineCount = 1;
            }
            if(order.getPayType() == 2){//现金支付
                cashAmount = order.getPrice();
                cashCount =  1;
            }
            if(order.getPayType() == 3){//刷卡支付
                swipeAmount = order.getPrice();
                swipeCount = 1;
            }
            if(order.getPayType() == 4){//支付宝支付
                alipayAmount = order.getPrice();
                alipayCount = 1;
            }
            orderStats2.setStoreId(order.getStoreId());
            orderStats2.setYear(year);
            orderStats2.setMonth(month);
            orderStats2.setDay(today);
            orderStats2.setAmount(amount);
            orderStats2.setCount(count);
            orderStats2.setWriteoffAmount(writeoffAmount);
            orderStats2.setWriteoffCount(writeoffCount);
            orderStats2.setCashAmount(cashAmount);
            orderStats2.setCashCount(cashCount);
            orderStats2.setSwipeAmount(swipeAmount);
            orderStats2.setSwipeCount(swipeCount);
            orderStats2.setOnlineAmount(onlineAmount);
            orderStats2.setOnlineCount(onlineCount);
            orderStats2.setAlipayAmount(alipayAmount);
            orderStats2.setAlipayCount(alipayCount);
            orderStats2Mapper.insert(orderStats2);
        }
        //解锁
        redisUtils.unLock(key);
        return orderStats2;
    }

    @Override
    public OrderStats2 getStats2(long storeId, String year, String month, String day) {

        // 查询条件
        QueryWrapper<OrderStats2> queryWrapper = new QueryWrapper();
        queryWrapper.eq("store_id", storeId);
        if(StringUtils.isEmpty(year) && StringUtils.isEmpty(month) && StringUtils.isEmpty(day)){
            //查询当天的业绩
            String today = DateUtils.formatDate(new Date(),"yyyy-MM-dd");
            queryWrapper.eq("day", today);
            year = DateUtils.formatYear();
            month = today.substring(0,7);
            day = today;
        }
        if(StringUtils.isNotEmpty(year))queryWrapper.eq("year", year);
        if(StringUtils.isNotEmpty(month)) queryWrapper.eq("month", month);
        if(StringUtils.isNotEmpty(day))queryWrapper.eq("day", day);
        queryWrapper.orderByDesc("ct");
        queryWrapper.last("LIMIT 1");
        // 查询
        OrderStats2 orderStats2 = orderStats2Mapper.selectOne(queryWrapper);
        // 还没有营业数据时
        if(orderStats2 == null) {
            orderStats2 = new OrderStats2();
            orderStats2.setStoreId(storeId);
            orderStats2.setYear(year);
            orderStats2.setMonth(month);
            orderStats2.setDay(day);
            orderStats2.setAmount(0);
            orderStats2.setCount(0);
            orderStats2.setWriteoffAmount(0);
            orderStats2.setWriteoffCount(0);
            orderStats2.setCashAmount(0);
            orderStats2.setCashCount(0);
            orderStats2.setSwipeAmount(0);
            orderStats2.setSwipeCount(0);
            orderStats2.setOnlineAmount(0);
            orderStats2.setOnlineCount(0);
            orderStats2.setAlipayAmount(0);
            orderStats2.setAlipayCount(0);
        }
        return orderStats2;
    }
}
